﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace InterfazWI
{
    public class Servidor
    {
        TcpListener server = null;
        public void startServer(IPAddress ip, int puerto)
        {
            server = new TcpListener(ip, puerto);
            // Start listening for client requests.
            server.Start();
        }

        public void startListener()
        {
            // Buffer for reading data
            Byte[] bytes = new Byte[256];
            String data = null;

            while (true)
            {
                TcpClient client = server.AcceptTcpClient();
                NetworkStream stream = client.GetStream();
                int i;
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);


                    // Process the data sent by the client.
                    data = data.ToUpper();

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);

                }

                // Shutdown and end connection
                client.Close();
            }

        }

        public void stopServer()
        {
            server.Stop();
        }
    }
}
